{extend name="base" /}
<!-- page specific plugin styles -->
{block name="plugin-styles"}{/block}
<!-- inline styles related to this page -->
{block name="inline-styles"}
<link rel="stylesheet" href="/static/ace/assets/css/colorbox.min.css" />
{/block}
<!-- breadcrumb -->
{block name="breadcrumbs"}
<ul class="breadcrumb">
    <li>
        <i class="ace-icon fa fa-home home-icon"></i>
        <a href="{:url('Index/index')}">首页</a>
    </li>
    <li class="active">代理管理</li>
</ul>
<!-- /.breadcrumb -->
{/block}
<!-- PAGE CONTENT BEGINS -->
{block name="content"}
<div class="page-header">
    <h1>
		代理列表
	</h1>
</div>
<!-- /.page-header -->
<div class="clearfix tableTools-container">
    <form class="pull-left form-inline">
        <div class="form-group">
            <label class="sr-only" for="title">代理</label>
            <div class="input-group">
                <span class="input-group-addon">代理</span>
                <input type="text" class="form-control input-sm" name="title" placeholder="姓名,手机号,微信号">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label" for="title">级别</label>
            <select class="form-control input-sm" name="grade" id="">
                <option value="0">全部</option>
                <option value="1">品正商</option>
                <option value="2">品正商V</option>
            </select>
        </div>
        <div class="form-group">
            <label class="control-label" for="title">审核状态</label>
            <select class="form-control input-sm" name="status" id="">
                <option value="0">全部</option>
                <option value="1">已通过</option>
                <option value="2">待审核</option>
                <option value="3">未通过</option>
            </select>
        </div>
        <div class="form-group">
        	<button type="button" class="btn btn-xs btn-primary" id="searchTable">搜索</button>
        	<button type="reset" class="btn btn-xs btn-primary" >重置</button>
        </div>
        <div class="form-group">
            &nbsp;&nbsp;&nbsp;&nbsp;<a target='_blank' href='/api/v1/tools/qrcode.html?url={$reg_url}'>一级注册二维码</a>
        </div>
    </form>
</div>
<div class="table-responsive">
	<table class="table table-bordered" id="DynamicTable"></table>
</div>
<!-- /.table-responsive -->

<!-- 详情Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header no-padding">
                <div class="table-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    代理详情
                </div>
            </div>
            <div class="modal-body no-padding">
                <table class="table table-striped table-bordered no-margin-bottom no-border-top">
                    <tbody>
                        <tr>
                            <th class="text-right" width="30%">姓名</th>
                            <td id="name"></td>
                        </tr>
                        <tr>
                            <th class="text-right">充值账户-余额</th>
                            <td id="account"></td>
                        </tr>
                        <tr>
                            <th class="text-right">充值账户(锁定金额)</th>
                            <td id="account_lock"></td>
                        </tr>
                        <tr>
                            <th class="text-right">奖励账户</th>
                            <td id="rewardaccount"></td>
                        </tr>
                        <tr>
                            <th class="text-right">奖励账户(锁定金额)</th>
                            <td id="rewardaccount_lock"></td>
                        </tr>
                         <tr>
                            <th class="text-right">押金账户</th>
                            <td id="yajinaccount"></td>
                        </tr>
                        <tr>
                            <th class="text-right">电话</th>
                            <td id="tel"></td>
                        </tr>
                        <tr>
                            <th class="text-right">微信号</th>
                            <td id="weixin"></td>
                        </tr>
                        <tr>
                            <th class="text-right">邮箱</th>
                            <td id="email"></td>
                        </tr>
                        <tr>
                            <th class="text-right">身份证</th>
                            <td id="idnum"></td>
                        </tr>
                        <tr>
                            <th class="text-right">所在地</th>
                            <td id="addrvalue"></td>
                        </tr>
                        <tr>
                            <th class="text-right">详细地址</th>
                            <td id="addr"></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button class="btn btn-sm btn-danger pull-right" data-dismiss="modal"> <i class="ace-icon fa fa-times"></i> Close </button>
            </div>
        </div>
    </div>
</div>

<!-- 充值 Modal -->
<div class="modal fade" id="myModalRecharge" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header no-padding">
                <div class="table-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    充值
                </div>
            </div>
            <div class="modal-body no-padding">
                
                <table class="table table-bordered no-margin-bottom no-border-top">
                    <tbody>
                        <tr>
                            <th class="text-right" width="20%">金额</th>
                            <td><input type="number" class="form-control input-sm" name="money" id="money"></td>
                        </tr>
                        <tr>
                            <th class="text-right">备注</th>
                            <td><textarea class="form-control input-sm" id="remark"></textarea></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button id = 'rechargeBtn' class="btn btn-sm btn-success" onClick="recharge()"> <i class="ace-icon fa fa-check"></i> save </button>
                <button  class="btn btn-sm btn-danger pull-right" data-dismiss="modal"> <i class="ace-icon fa fa-times"></i> Close </button>
            </div>
        </div>
    </div>
</div>

<!-- 首次充值 Modal -->
<div class="modal fade" id="myFirstModalRecharge" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header no-padding">
                <div class="table-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    注册充值
                </div>
            </div>
            <div class="modal-body no-padding">
                
                <table class="table table-bordered no-margin-bottom no-border-top">
                    <tbody>
                        <tr>
                            <th class="text-right" width="20%">充值金额</th>
                            <td><input type="number" class="form-control input-sm" name="money" id="first_money"></td>
                        </tr>
                        <tr>
                            <th class="text-right" width="20%">押金金额</th>
                            <td><input type="number" class="form-control input-sm" name="yajin" id="yajin"></td>
                        </tr>
                        <tr>
                            <th class="text-right">备注</th>
                            <td><textarea class="form-control input-sm" id="first_remark">注册充值</textarea></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button id = 'firstRechargeBtn' class="btn btn-sm btn-success" onClick="firstRecharge()"> <i class="ace-icon fa fa-check"></i> 保存 </button>
                <button class="btn btn-sm btn-danger pull-right" data-dismiss="modal"> <i class="ace-icon fa fa-times"></i> Close </button>
            </div>
        </div>
    </div>
</div>


<!-- 更换代理人 Modal -->
<div class="modal fade" id="myModalAgent" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header no-padding">
                <div class="table-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    更换代理人
                </div>
            </div>
            <div class="modal-body no-padding">
                
                <table class="table table-bordered no-margin-bottom no-border-top">
                    <tbody>
                        <tr>
                            <th class="text-right" width="20%">代理ID</th>
                            <td><input type="number" class="form-control input-sm" name="pid" id="pid"></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button class="btn btn-sm btn-success" onClick="update_agent()"> <i class="ace-icon fa fa-check"></i> save </button>
                <button class="btn btn-sm btn-danger pull-right" data-dismiss="modal"> <i class="ace-icon fa fa-times"></i> Close </button>
            </div>
        </div>
    </div>
</div>
{/block}
<!-- PAGE CONTENT ENDS -->
<!-- page specific plugin scripts -->
{block name="plugin-scripts"}
<script src="/static/ace/assets/js/jquery.dataTables.min.js"></script>
<script src="/static/ace/assets/js/jquery.dataTables.bootstrap.min.js"></script>
<script src="/static/ace/assets/js/jquery.colorbox.min.js"></script>
{/block}
<!-- inline scripts related to this page -->
{block name="related-scripts"}
<script type="text/javascript">
	var myTable = 
	$('#DynamicTable')
	.wrap("<div class='dataTables_borderWrap' />")   //if you are applying horizontal scrolling (sScrollX)
	.DataTable({
		"autoWidth": false,	//智能列宽处理
		"deferRender": true,	//延迟渲染以获得额外的初始化速度
		"info": true,	//表信息显示
		"lengthChange": false,	//更改每页记录数
		"ordering": false,	//排序功能
		"paging": true,	//启用或禁用表分页
		"processing": false,	//处理指标
		// "scrollX": false,	//水平滚动
		// "scrollY": false,	//垂直滚动
		"searching": false,	//搜索功能
		"serverSide": true,	//服务器端处理模式
		"stateSave": false,	//状态保存(页面重新加载时恢复表状态)
		"language": {
            "url": "//cdn.datatables.net/plug-ins/1.10.19/i18n/Chinese.json"
        },
		"ajax": {
            "url": "/admin/agent/lists",
            "type": "POST",
            "data": function (d) {
                d.title = $("input[name=title]").val();
                d.grade = $("select[name=grade]").val();
                d.status = $("select[name=status]").val();
            },
            "dataSrc": function (json) {
                return json.data;
            }
        },
        "columns": [{
            "title": "授权编号",
            "data": "id"
        }, {
            "title": "姓名",
            "data": "name"
        }, {
            "title": "电话",
            "data": "tel"
        }, {
            "title": "微信号",
            "data": "weixin"
        }, {
            "title": "邮箱",
            "data": "email"
        }, {
            "title": "保证金截图",
            "data": "idpic2",
        }, {
            "title": "状态",
            "data": "status"
        }, {
            "title": "余额",
            "data": "account"
        }, {
            "title": "押金",
            "data": "yajinaccount"
        }, {
            "title": "级别",
            "data": "grade"
        }, {
            "title": "上级",
            "data": "fname"
        }, {
            "title": "申请时间",
            "data": "addtime"
        }, {
            "title": "操作",
            "data": "id"
        }],
        "columnDefs": [{
        	"className": "text-center",
        	"targets": [0,6,7,8,10]
        },{
            "render": function (data, type, row, meta) {
                return '<a class="btn btn-minier btn-warning" href="'+data+'" data-rel="colorbox">查看截图</a>';
            },
            "targets": 5
        }, {
            "render": function (data, type, row, meta) {
                if(data==1){
                    return '<span class="label label-success">已审核</span>';
                }else if(data==2){
                    return '<span class="label label-warning">待审核</span>';
                }else if(data==4){
                    return '<span class="label label-warning">已关停</span>';
                }else{
                    return '<span class="label label-danger">未通过</span>';
                }
            },
            "targets": 6
        }, {
            "render": function (data, type, row, meta) {
                if(data==1){
                    return "品正商"
                }else if(data==2){
                    return "品正商V"
                }
            },
            "targets": 9
        }, {
        	"render": function (data, type, row, meta) {
                return '<div class="btn-group">\
					<a target="_blank" class="btn btn-xs btn-success" data-rel="tooltip" title="查看详情" href="{:url('agent/detail')}?mid='+row.id+'">\
                        <i class="ace-icon fa fa-search-plus bigger-120"></i>\
                    </a>\
					<a data-toggle="modal"  class="btn btn-xs btn-pink" data-rel="tooltip" title="重置密码" onClick="reset('+data+')">\
				        <i class="ace-icon fa fa-pencil bigger-120"></i>\
                    </a>\
					<a data-toggle="modal" data-target="#myFirstModalRecharge" class="btn btn-xs btn-pink" data-rel="tooltip" title="注册充值" onClick="showFirstModalRecharge('+data+')">\
                    <i class="ace-icon fa glyphicon-euro bigger-120"></i>\
                    </a>\
                    <a data-toggle="modal"  class="btn btn-xs btn-danger" data-rel="tooltip" title="关停" onClick="doClose('+data+')">\
                    <i class="ace-icon fa fa-trash-o bigger-120"></i>\
					</a>\
					<button data-toggle="modal" data-target="#myModalAgent" class="btn btn-xs btn-warning" data-rel="tooltip"  title="修改上级" onClick="showAgentModal('+data+')">\
						<i class="ace-icon fa fa-user bigger-120"></i>\
					</button>\
					<a data-toggle="modal" data-target="#myModalRecharge" class="btn btn-xs btn-pink" data-rel="tooltip" title="充值" onClick="showModalRecharge('+data+')">\
						<i class="ace-icon fa fa-euro bigger-120"></i>\
					</a>\
					<a class="btn btn-xs btn-pink" target="_blank" data-rel="tooltip"  title ="添加地址" href="{:url('agent/address_add')}?mid='+row.id+'">\
                        <i class="ace-icon fa fa-plus bigger-120"></i>\
                    </a>\
                </a>\
				</div>';
            },
            "targets": 12
        }],
        "initComplete": function( settings, json ) {
        	$("#DynamicTable").prev(".row").remove();
        	//add tooltip for small view
			$('[data-rel="tooltip"]').tooltip()
            $('[data-rel="colorbox"]').colorbox(colorbox_params);;
		}
	});

    $("#searchTable").click(function () {
        myTable.draw();
    });
    
    var $overflow = '';
    var colorbox_params = {
        rel: 'colorbox',
        reposition:true,
        scalePhotos:true,
        scrolling:false,
        previous:'<i class="ace-icon fa fa-arrow-left"></i>',
        next:'<i class="ace-icon fa fa-arrow-right"></i>',
        close:'&times;',
        current:'{current} of {total}',
        maxWidth:'100%',
        maxHeight:'100%',
        onOpen:function(){
            $overflow = document.body.style.overflow;
            document.body.style.overflow = 'hidden';
        },
        onClosed:function(){
            document.body.style.overflow = $overflow;
        },
        onComplete:function(){
            $.colorbox.resize();
        }
    };

    
    $("#cboxLoadingGraphic").html("<i class='ace-icon fa fa-spinner orange fa-spin'></i>");//let's add a custom loading icon
    
    
    $(document).one('ajaxloadstart.page', function(e) {
        $('#colorbox, #cboxOverlay').remove();
    });
    
    // 查看
    function showModal(_id){
        $.ajax({
            url: '/admin/agent/find',
            type: 'GET',
            dataType: 'json',
            data: {id: _id},
        })
        .done(function(json) {
            console.log("success");
            initPageHTML('myModal',json.data);
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }
    // 显示充值框
    function showModalRecharge(_id){
    	$("#remark").val('');
    	$('#rechargeBtn').attr("disabled", false);
        $("#myModalRecharge").data('id',_id);
    }
    
    //第一次充值 
    function showFirstModalRecharge(_id){
    	$('#firstRechargeBtn').attr("disabled", false);
    	$("#yajin").val('');
    	$("#remark").val('');
    	 $("#myFirstModalRecharge").data('id',_id);
    }
    // 充值
    function recharge() {
    	$('#rechargeBtn').attr("disabled", true);
        // body...
        $.ajax({
            url: "/admin/recharge/add",
            type: 'POST',
            dataType: 'json',
            data: {
                mid: $("#myModalRecharge").data("id"),
                money: $("#money").val(),
                remark: $("#remark").val()
            },
        })
        .done(function(json) {
            console.log("success");
            if(json.returnCode==1){
            	
                alert("充值成功");
                $("#myModalRecharge").modal('hide');
                myTable.draw();
                
            }else{
                alert(json.returnMsg);
            }
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }
    
    function firstRecharge(){
    	$('#firstRechargeBtn').attr("disabled", true);
    	
    	 // body...
        $.ajax({
            url: "/admin/recharge/first_add",
            type: 'POST',
            dataType: 'json',
            data: {
                mid: $("#myFirstModalRecharge").data("id"),
                money: $("#first_money").val(),
                remark: $("#first_remark").val(),
                yajin: $("#yajin").val()
            },
        })
        .done(function(json) {
            console.log("success");
            if(json.returnCode==1){
            	
                alert("充值成功");
                $("#myFirstModalRecharge").modal('hide');
                myTable.draw();
            }else{
                alert(json.returnMsg);
            }
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }
    
    // 关停
    function doClose(_id){
        if(!confirm("确认关停该数据？")){
            return;
        }
        $.ajax({
            url: "/admin/agent/close",
            type: 'POST',
            dataType: 'json',
            data: {
            	id:_id,
            },
        })
        .done(function(json) {
            console.log("success");
            if(json.returnCode==1){
            	alert(json.returnMsg); 
            	myTable.draw();
            }else{
                alert(json.returnMsg);
            }
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }
    
    function reset(_id){
    	if(!confirm("确认要重置密码？")){
    		return;
    	}
    	$.ajax({
            url: "/admin/agent/reset_pwd",
            type: 'POST',
            dataType: 'json',
            data: {
                mid:_id,
            },
        })
        .done(function(json) {
            console.log("success");
            if(json.returnCode==1){
                alert("重置成功");
            }else{
                alert(json.returnMsg);
            }
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }

    // 显示更换代理人
    function showAgentModal(_id){
        $("#myModalAgent").data('id',_id);
    }

    function update_agent(){
        $.ajax({
            url: "/admin/agent/update_agent",
            type: 'POST',
            dataType: 'json',
            data: {
                mid: $("#myModalAgent").data("id"),
                pid: $("#pid").val()
            },
        })
        .done(function(json) {
            console.log("success");
            if(json.returnCode==1){
                alert("操作成功");
                myTable.draw();
                $("#myModalAgent").modal('hide');
            }else{
                alert(json.returnMsg);
            }
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }
</script>
{/block}